package com.yujinglin.AirClassify;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class AirClassifyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private Text outKey = new Text();
    private IntWritable one = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] info = line.split(",");

        if (info.length >= 19 && !info[16].isEmpty()) {

                int distanceCode = -1;
                String distance="无数据";

          try {
              distanceCode=Integer.parseInt(info[17]);
          }catch (Exception e){
              e.printStackTrace();
          }

            if(distanceCode<500&&distanceCode>=0){
                distance="500以内的短程航班";
            }else if(distanceCode>=500&&distanceCode<1000){
                distance="500以上的短程航班";
            }else if(distanceCode>=1000&&distanceCode<1500){
                distance="1500以内的中程航班";
            }else if(distanceCode>=1500&&distanceCode<2000){
                distance="1500以上的中程航班";
            }else if(distanceCode>=2000&&distanceCode<2500){
                distance="远程航班";
            }else if(distanceCode>2500){
                distance="全球航班";
            }



            outKey.set(distance); // 设置输出的key为距离分类
                context.write(outKey, one); // 输出到context，每个记录计数为1

        }

    }
}